﻿using System.Collections.Generic;
using Glorysoft.Auto.Xfw;
using Glorysoft.EAP.Core.Entities;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Glorysoft.EAP.Core
{
    public static class JsonUtils
    {
       
        public static Equipment GetEqpCfg(this JObject obj, bool isRoot)
        {
            var json = isRoot ? obj.GetBody() : obj;
            var eqp = new Equipment
            {
                EqpID = json.GetVal(CItems.EqpID),
                EqpType = json.GetVal(CItems.EqpType),
            };
            return eqp;
        }
        public static List<Equipment> GetMultiEqpCfg(this JObject obj)
        {
            var json = obj.GetBody();
            var list = json.GetList(CItems.EqpList);
            var eqpList = new List<Equipment>();
            foreach (var item in list)
            {
                var temp = item as JObject;
                var eqp = temp.GetEqpCfg(false);
                eqpList.Add(eqp);
            }
            return eqpList;
        }

        public static EqpStatusInfo GetEqpStatusInfo(this JObject obj)
        {
            var json = obj.GetBody();
            var eqpStatusInfo = new EqpStatusInfo
            {
                ZoneTemp = json.GetVal(CItems.ZoneTemp),
                ScrewCnt = json.GetVal(CItems.ScrewCnt),
                IsRcmCmp = json.GetVal(CItems.IsRcpCmp),
                LastPMCollTime = json.GetVal(CItems.LastPMColTime),
                RecipeID = json.GetVal(CItems.RecipeName),
                RepairID = json.GetVal(CItems.RepairID),
                BuyOffID = json.GetVal(CItems.BuyOffID),
            };
            return eqpStatusInfo;
        }
        public static AlarmInfo GetAlarmInfo(this JObject obj, bool isRoot)
        {
            var json = isRoot ? obj.GetBody() : obj;

            var alarmInfo = new AlarmInfo
            {
                AlarmID = json.GetVal(CItems.AlarmID),
                AlarmCode = json.GetVal<int>(CItems.AlarmCode)
            };
            return alarmInfo;
        }
        public static List<AlarmInfo> GetAlarmList(this JObject obj)
        {
            var json = obj.GetBody();
            var list = json.GetList(CItems.List);
            var alarmList = new List<AlarmInfo>();
            foreach (var item in list)
            {
                var temp = item as JObject;
                var alarmInfo = temp.GetAlarmInfo(false);
                alarmList.Add(alarmInfo);
            }
            return alarmList;
        }
        public static List<EqpParameters> GetVIDList(this JObject obj)
        {
            var json = obj.GetBody();
            var list = json.GetList(CItems.VIDList);
            var alarmList = new List<EqpParameters>();
            foreach (var item in list)
            {
                var temp = item as JObject;
                var info = temp.GetVIDInfo();
                alarmList.Add(info);
            }
            return alarmList;
        }
        public static EqpParameters GetVIDInfo(this JObject obj)
        {
            var json = obj;
            var para = new EqpParameters
            {
                ID = json.GetVal(CItems.VID),
                IDType = json.GetVal(CItems.IDType),
                Name = json.GetVal(CItems.Name),
                Value = json.GetVal(CItems.Value)
            };
            return para;
        }

        //public static Return GetReturnObject(this JObject obj)
        //{
        //    var json = obj.GetReturn();
        //    var rr = new Return()
        //    {
        //        ReturnCode = json.GetVal(CItems.ReturnCode),
        //        ReturnMessage = json.GetVal(CItems.ReturnMessage)
        //    };
        //    return rr;
        //}

        public static string Serialization(object msg)
        {
            return JsonConvert.SerializeObject(msg);
        }

        public static string Serialization(JObject obj)
        {
            return JsonConvert.SerializeObject(obj);
        }
        //public static T DeserializeObject<T>(string str)
        //{
        //    return JsonConvert.DeserializeObject<T>(str);
        //}

        //public static NATSData ParseHeader(INATSContext ctx, JObject json)
        //{
        //    var header = json.GetHeaderObject();
        //    var messageName = header.MessageName;
        //    messageName = $"{header.MachineName}.{messageName}";
        //    var data = new NATSData(messageName)
        //    {
        //        ConnName = ctx.Name,
        //        Context = ctx,
        //        Data = json
        //    };
        //    return data;
        //}
    }
}
